package com.example.kuaizu.mq;

import com.example.kuaizu.exception.SendMassageExcepTion;
import com.example.kuaizu.job.dto.EveryProjectJoinContDTO;
import com.example.kuaizu.mapper.ProjectMapper;
import com.example.kuaizu.mapper.SubscribeMassageMapper;
import com.example.kuaizu.pojo.ProjectReview;
import com.example.kuaizu.pojo.dto.MassageSendDTO;
import com.example.kuaizu.pojo.dto.MassageSubDTO;
import com.example.kuaizu.pojo.ov.ProjectOv;
import com.example.kuaizu.pojo.pram.JoinSubscribeMassageParam;
import com.example.kuaizu.utils.WxRequestUtils;
import kuaizu.common.constp.KuaiZuConst;
import org.springframework.amqp.core.Message;
import kuaizu.common.massage.MassageTemplateConst;
import kuaizu.common.mqconst.MQConstant;
import kuaizu.common.param.projectReviewMassageParam;
import kuaizu.common.template.ProjectPromotionTemp;
import kuaizu.common.template.ProjectReviewMassageTemp;
import kuaizu.common.template.ProjectYaoQingMassageTemp;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Objects;

/**
 * @author: wangmx15
 * @date: 2025/8/24
 **/
@Component
@RequiredArgsConstructor
@Log4j2
public class MQBuils {
    private final SubscribeMassageMapper subscribeMassageMapper;
    private final WxRequestUtils wxRequestUtils;
    private final ProjectMapper projectMapper;

    /**
     * 投递项目之后,给项目的创建者发送消息  定时消息触发任务通知
     *
     * @param id 项目id
     */
    @RabbitListener(queues = MQConstant.PROJECT_CREATE_SUBSCRIBE_QUEUE)
    public void sendProjectMessageDlxQueues(EveryProjectJoinContDTO rep,Message message) {
        //根据id去查询是否要推广的信息 项目id+推广状态确定项目创建者的id,推广给项目创建者
        Integer id = rep.getProjectId();
        MassageSubDTO massageSubDTO = subscribeMassageMapper.selectSubscribeMassageById(id, MassageTemplateConst.MASSAGE_TYPE_DELIVERY);
        if (ObjectUtils.isEmpty(massageSubDTO)) {
            log.error("sendProjectMessageDlxQueues->查询不到对应的信息:{},请求参数{}", massageSubDTO, rep);
            return;
        }
        if (null == massageSubDTO.getProjectName()) {
            //根据项目id查询项目信息
            ProjectOv projectOv = projectMapper.selectProjectById(id.toString());
            if (ObjectUtils.isEmpty(projectOv)) {
                log.error("sendProjectMessageDlxQueues->项目不存在:{}", massageSubDTO);
                sendSubscribeMessage(massageSubDTO.getId());
                return;
            }else{
                massageSubDTO.setProjectName(projectOv.getProjectName());
            }
        }
        log.info("sendProjectMessageDlxQueues->mq发送邮件推广信息:{}", massageSubDTO);
      massageSubDTO.setCount(rep.getCount());
        //创建模版对象
        ProjectPromotionTemp projectPromotionTemp = new ProjectPromotionTemp();
        projectPromotionTemp.setTemplateParam(massageSubDTO.getProjectName(), massageSubDTO.getCount());
        try {
            MassageSendDTO sendParam = getSendParam(MassageTemplateConst.PROJECT_DELIVERY_TEMPLATE_ID, massageSubDTO.getOpenId(), massageSubDTO.getSubPathUrl(), projectPromotionTemp);
            wxRequestUtils.sendMassage(sendParam);
        } catch (SendMassageExcepTion e) {
            throw e;
        }
        log.info("sendProjectMessageDlxQueues->发送项目投递订阅消息:{}", projectPromotionTemp);
        sendSubscribeMessage(massageSubDTO.getId());
    }


    /**
     * 项目审核通知 管理员审核项目通过之后发送的消息 后台管理员发送消息通知
     *
     * @param id
     */
    @RabbitListener(queues = MQConstant.PROJECT_AUDIT_QUEUE)
    public void projectReviewMassageQueues(projectReviewMassageParam param, Message message) {
        //项目审核结果 项目id+类型确定项目人的地址
        //根据项目id查询项目信息
        MassageSubDTO massageSubDTO = subscribeMassageMapper.selectSubscribeMassageById(param.getProjectId(), MassageTemplateConst.MASSAGE_TYPE_AUDIT);
        //根据id查询项目审核信息
        //ProjectReview projectReview = projectMapper.selectById(param.getProjectId(),param.getOpenId());
        if (ObjectUtils.isEmpty(massageSubDTO)) {
            log.error("projectReviewMassageQueues->查询不到对应的信息:{},请求参数{}", massageSubDTO, param);
            return;
        }
        log.info("projectReviewMassageQueues->mq发送订阅推广信息:{}", massageSubDTO);
        if (ObjectUtils.isEmpty(massageSubDTO)) {
            log.error("projectReviewMassageQueues->mq发送邮件推广信息失败:{}", massageSubDTO);
            //throw new MassageException("找不到id的相关数据"+ param.getProjectId());
        }
        //创建模版对象
        ProjectReviewMassageTemp projectReviewMassageTemp = new ProjectReviewMassageTemp();
        try {
            projectReviewMassageTemp.setTemplateParam(massageSubDTO.getProjectName(), param.isStatus(), param.getText());
            MassageSendDTO sendParam = getSendParam(MassageTemplateConst.AUDIT_PASS_TEMPLATE_ID, massageSubDTO.getOpenId(), massageSubDTO.getSubPathUrl(), projectReviewMassageTemp);
            wxRequestUtils.sendMassage(sendParam);
        } catch (SendMassageExcepTion e) {
            throw e;
        }

        log.info("projectReviewMassageQueues->保存项目审核订阅消息{}", projectReviewMassageTemp);
        sendSubscribeMessage(massageSubDTO.getId());
    }

    /**
     * 申请加入项目订阅  投递项目时的订阅弹窗 项目创建者同意或者拒绝加入项目通知
     *
     * @param id
     */
    @RabbitListener(queues = MQConstant.PROJECT_JOIN_QUEUE)
    public void projectReviewShenHeMassageQueues(JoinSubscribeMassageParam param ,Message message) {
        //项目审核结果 项目id+类型确定项目人的地址
        //根据项目id查询项目信息
        MassageSubDTO massageSubDTO = subscribeMassageMapper.selectSubscribeMassageByIdAndOpenId(param.getOpenId(), param.getProjectId(), MassageTemplateConst.MASSAGE_TYPE_PROJECT_AUDIT);
        //根据id查询项目审核信息
        ProjectReview projectReview = projectMapper.selectByIdAndOpenId(param.getProjectId(), param.getOpenId());
        if (ObjectUtils.isEmpty(massageSubDTO) || ObjectUtils.isEmpty(projectReview)) {
            log.error("projectReviewShenHeMassageQueues->查询不到对应的信息:{},请求参数{}", massageSubDTO, param);
            return;
        }
        boolean staurs = (Objects.equals(projectReview.getIsApproved(), KuaiZuConst.CHECKPASS));
        if (ObjectUtils.isEmpty(massageSubDTO)) {
            log.error("projectReviewShenHeMassageQueues->mq发送邮件推广信息失败:{}", massageSubDTO);
            //throw new MassageException("找不到id的相关数据"+ param.getProjectId());
        }
        log.info("-projectReviewShenHeMassageQueues->projectReviewShenHeMassageQueues->mq发送邮件推广信息:{}", massageSubDTO);
        //创建模版对象
        ProjectYaoQingMassageTemp projectReviewMassageTemp = new ProjectYaoQingMassageTemp();
        projectReviewMassageTemp.setTemplateParam(massageSubDTO.getProjectName(), staurs, param.getName());
        try {
            MassageSendDTO sendParam = getSendParam(MassageTemplateConst.SUBSCRIBE_AUDIT_TEMPLATE_ID, massageSubDTO.getOpenId(), massageSubDTO.getSubPathUrl(), projectReviewMassageTemp);
            wxRequestUtils.sendMassage(sendParam);
        } catch (SendMassageExcepTion e) {
            throw e;
        }
        log.info("projectReviewShenHeMassageQueues->发送项目审核订阅消息{}", projectReviewMassageTemp);
        sendSubscribeMessage(massageSubDTO.getId());
    }

    //发送完成消息设置数据库的消息状态已发送 0为发送
    public void sendSubscribeMessage(Integer id) {
        //根据id修改数据库内容
        subscribeMassageMapper.updateSubscribeMassageStatus(id);
    }


    public MassageSendDTO getSendParam(String templateId, String openid, String pageUrl, Object dat) {
        //获取token
        String accessToken = wxRequestUtils.getAccessToken();
        MassageSendDTO massageSendDTO = new MassageSendDTO();
        massageSendDTO.setAccessToken(accessToken);
        //模版id
        massageSendDTO.setTemplate_id(templateId);
        massageSendDTO.setTouser(openid);
        massageSendDTO.setPage(pageUrl);
        massageSendDTO.setData(dat);
        massageSendDTO.setMiniprogram_state(KuaiZuConst.MINI_PROGRAM_TYPE_TRIAL);
        massageSendDTO.setLang(KuaiZuConst.MINI_PROGRAM_LANGUAGE_ZH_CN);
        return massageSendDTO;
    }
}
